//
//  CloseNewsView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/24.
//

import UIKit

class CloseNewsView: UIView {
    private let contentView = UIView()

    init(_ frame:CGRect,x:CGFloat){
        super.init(frame: UIScreen.main.bounds)
        UIApplication.shared.delegate?.window??.addSubview(self)
        backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
        addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(removeView(tap:))))
        let offy = frame.origin.y+frame.height
        addSubview(contentView)
        contentView.snp.makeConstraints({
            if offy > ScreenHeight/2 {
                $0.bottom.equalToSuperview().inset(ScreenHeight-offy+frame.height)
            }else{
                $0.top.equalTo(offy)
            }
            $0.left.equalTo(15)
            $0.right.equalToSuperview().inset(15)
        })
        
        contentView.backgroundColor = .clear
        let maskView = CloseNewsContentView(x: x,isUp: offy>ScreenHeight/2 ? false:true)
        contentView.addSubview(maskView)
        maskView.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        var list:Array<CloseMenuItem> = []
        list.append(CloseMenuItem.init(title: "不感兴趣", subTitle: "减少这类内容", imageName: "border_close", item: []))
        list.append(CloseMenuItem.init(title: "举报", subTitle: "标题夸张，内容质量差 等", imageName: "jubao", item: [CloseMenuItem()]))
        list.append(CloseMenuItem.init(title: "拉黑作者:默默", subTitle: "", imageName: "lahei", item: []))
        list.append(CloseMenuItem.init(title: "屏蔽", subTitle: "财经", imageName: "pingbi", item: [CloseMenuItem()]))
        var temp:UIView?
        for i in 0..<list.count {
            let su = CloseNewsSubView(item: list[i])
            su.backgroundColor = .clear
            contentView.addSubview(su)
            su.snp.makeConstraints({
                if temp == nil{
                    $0.top.equalTo(offy>ScreenHeight/2 ? 0:10)
                }else{
                    $0.top.equalTo(temp!.snp.bottom)
                }
                $0.left.right.equalTo(0)
                if i == list.count-1{
                    $0.bottom.equalToSuperview().inset(offy>ScreenHeight/2 ? 10:0)
                }
            })
            if i<list.count-1 {
                let line = UIView.SeparatorLine()
                contentView.addSubview(line)
                line.snp.makeConstraints({
                    $0.left.equalTo(15)
                    $0.right.equalToSuperview().inset(15)
                    $0.top.equalTo(su.snp.bottom)
                    $0.height.equalTo(0.5)
                })
            }
            temp = su
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let frame = contentView.frame
        contentView.frame = CGRect.init(x: frame.origin.x, y: frame.origin.y+frame.size.height, width: frame.size.width, height: 0)
        UIView.animate(withDuration: 0.3) {
            self.contentView.frame = frame
        }
    }
    
    @objc func removeView(tap:UITapGestureRecognizer) {
        var frame = contentView.frame
        frame.origin.y = ScreenHeight
        UIView.animate(withDuration: 0.4) {
            self.contentView.alpha = 0
            self.contentView.frame = frame
        } completion: { (res) in
            self.removeFromSuperview()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class CloseNewsContentView: UIView {
    private var tap_x:CGFloat = 0
    private var isUp = false
    init(x:CGFloat,isUp:Bool){
        super.init(frame: .zero)
        self.isUp = isUp
        setRadiusAndDirection(radius: 5)
        backgroundColor = .clear
        tap_x = x
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        setBackgroundImageView(rect)
    }

    func setBackgroundImageView(_ rect: CGRect){
        let height:CGFloat = rect.size.height
        let margin:CGFloat = 10.0
        var fm = rect
        fm.size.height = height-margin
        fm.origin.y = isUp ? margin : 0
        let path = UIBezierPath(roundedRect: fm, byRoundingCorners: [.allCorners], cornerRadii: CGSize.init(width: 10, height: 10))
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.fillColor = UIColor.white.cgColor
        self.layer.addSublayer(layer)
        let arrowPath = UIBezierPath.init()
        let x = Int(tap_x) - 15
        let y = Int(isUp ? 10 : height - 10)
        let leftPoint = CGPoint(x: x-50, y: y)
        let centerPoint = CGPoint(x:x, y:isUp ? 0:Int(height))
        var controlPoint = CGPoint(x: x-20, y: y)
        arrowPath.move(to: leftPoint)
        arrowPath.addQuadCurve(to: centerPoint, controlPoint: controlPoint)
        let rightPoint = CGPoint(x:x+20, y: y)
        controlPoint = CGPoint(x: x-10, y: y)
        arrowPath.move(to: centerPoint)
        arrowPath.addQuadCurve(to: rightPoint, controlPoint: controlPoint)
        arrowPath.addLine(to: leftPoint)
        let arrowLayer = CAShapeLayer()
        arrowLayer.path = arrowPath.cgPath
        arrowLayer.fillColor = UIColor.white.cgColor
        self.layer.addSublayer(arrowLayer)
    }
}
